/**
 $Id: tMessage.h 2 2012-04-04 15:07:32Z gerhardus1@gmail.com $
 Versioning: a.b.c a is a major release, b represents changes or new features, c represents bug fixes. 
 @version 1.0.0   06/10/2011    Gerhardus Muller     Script created

 @note

 @todo
 
 @bug

 Copyright Gerhardus Muller
 */

#if !defined( tMessage_defined_ )
#define tMessage_defined_

#include "json_spirit.h"
#include "utils/object.h"
#include "utils/unixSocket.h"

class recoveryLog;

class tMessage : public object
{
  // Definitions
  public:
    // ##tservpacket##v4.0nnnnnn{system params}([payload]|{payload})
    // nnnnnn is the packet length excluding the part from ##..nn
    static const int FRAME_HEADER_LEN = 25; // strlen(##tservpacket##v4.0nnnnnn)
    static const char* PROTOCOL_VERSION_NUMBER;
    static const char* FRAME_HEADER;
    enum eEventType { EV_UNKNOWN=0,EV_BASE=1,EV_SCRIPT=2,EV_PERL=3,EV_BIN=4,EV_URL=5,EV_RESULT=6,EV_WORKER_DONE=7,EV_COMMAND=8,EV_PERSISTENT=9,EV_ERROR=10 };
    enum eFdType { FD_SOCKET,FD_PIPE,FD_FILE };

    /**
     * Commands are always handled out of band and distributed to all workers if not handled 
     * directly by the nucleus
     * CMD_STATS=1
     * CMD_RESET_STATS=2
     * CMD_REOPEN_LOG=3
     * CMD_REREAD_CONF=4
     * CMD_EXIT_WHEN_DONE=5
     * CMD_TIMER_SIGNAL=6
     * CMD_CHILD_SIGNAL=7
     * CMD_SHUTDOWN=8
     * CMD_CONF=9              - configures the application
     * CMD_WORKER_CONF=10       - configures a worker
     * CMD_END_OF_QUEUE=11      - indicates the last event on the queue
     * CMD_PERSISTENT_APP=12    - command forwarded to the persistent app on the appropriate queue
     * CMD_DUMP_STATE=13
     * */
    enum eCommandType { CMD_NONE=0,CMD_STATS=1,CMD_RESET_STATS=2,CMD_REOPEN_LOG=3,CMD_REREAD_CONF=4,CMD_EXIT_WHEN_DONE=5,CMD_TIMER_SIGNAL=6,CMD_CHILD_SIGNAL=7,CMD_SHUTDOWN=8,CMD_CONF=9,CMD_WORKER_CONF=10,CMD_END_OF_QUEUE=11,CMD_PERSISTENT_APP=12,CMD_DUMP_STATE=13 };

    // Methods
  public:
    tMessage( );
    tMessage( eEventType type, const char* queue=NULL, const char* objName="tMessage" );
    tMessage( const char* body, const char* objName="tMessage" );
    tMessage( const tMessage& c );
    virtual ~tMessage( );
    virtual std::string toString( );

    std::string toStringBrief( );
    const char* typeToString( );
    const char* commandToString( );

    // access to system parameters
    std::string getParam( const char* name )                          {return std::string();}
    std::string& getTraceTimestamp( )                                 {return traceTimestamp;}
    std::string& getDestQueue( )                                      {return destQueue;}
    enum eCommandType getCommand( )                                   {return CMD_NONE;}
    
    // serialisation support
    int serialiseToFile( int fd )                                     {return 0;}

  private:
    void construct( );
    void parseBody( const char* body );

    // Properties
  public:
    static recoveryLog*             theRecoveryLog;      ///< the recovery log
    static logger                   staticLogger;        ///< class scope logger

  protected:
    std::string                     strSerialised;        ///< string version of object serialisation

  private:
    eEventType                      eventType;            ///< event sub-type
    unsigned int                    expiryTime;           ///< absolute expiry time in seconds since Jan 1970 or 0
    int                             lifetime;             ///< requested lifetime of the object in seconds - -1 if not applicable
    std::string                     destQueue;            ///< queue that is to service the event
    std::string                     traceTimestamp;       ///< the trace timestamp to be used for subsequent logging entries

    // non archived properties
    unsigned int                    queueTime;            ///< time at which the event was placed in the queue - to be used to calculate queuing times
};	// class tMessage

  
#endif // !defined( tMessage_defined_)
